package com.atguigui.leetcode1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 2784. 检查数组是否是好的
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/7/24 8:56
 */
public class P2784CheckIfArrayIsGood {
    public static void main(String[] args) {
        int[] nums = {1, 3, 3, 2};
        boolean good = new P2784CheckIfArrayIsGood().isGood(nums);
    }

    public boolean isGood(int[] nums) {


        if (nums.length < 2) {
            return false;
        }

        Integer[] num = new Integer[nums.length];
        for (int i = 0; i < nums.length; i++) {
            num[i] = nums[i];
        }
        // 倒序排序
        Arrays.sort(num, Comparator.reverseOrder());

        Integer a = num[0];
        Integer b = num[1];

        // 两种情况直接返回false
        if (a != b || num.length != a + 1) {
            return false;
        }

        Integer f = num[1];
        // 挨个查看是否是单调递增
        for (int i = 2; i < num.length; i++) {
            if (f - num[i] != 1) {
                return false;
            }
            f = num[i];
        }

        return true;

    }
}
